<?php

class Bid extends BaseActiveRecord
{
    public function tableName()
    {
        return '{{bids}}';
    }

    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    public function rules()
    {
        return array
        (
            array('rate, lot_id', 'required'),
            array('rate', 'CNumberValidator'),
        );
    }

    public function getMyBids($iUserId)
    {
        $aMyBids = array();

        $sSql = 'SELECT b.*, l.user_id AS lot_user_id FROM '.$this->tableName().' AS b INNER JOIN '.Lot::model()->tableName().' AS l ON(b.lot_id = l.lot_id AND UNIX_TIMESTAMP(l.date_expired) > UNIX_TIMESTAMP()) WHERE b.user_id = '.intval($iUserId).';';
        $aMyBidRows = Yii::app()->db->createCommand($sSql)->queryAll();

        if($aMyBidRows)
        {
            foreach ($aMyBidRows as $aRow)
            {
                $aMyBids[$aRow['lot_id']] = $aRow;

                if($aRow['is_like'])
                {
                    $aMyBids[$aRow['lot_id']]['user'] = User::model()->with('profile')->findByPk($aRow['lot_user_id']);
                }
            }
        }

        return $aMyBids;
    }

    public function attributeLabels()
    {
        return array
        (
            'rate' => 'Предложить курс',
            'message'  => 'Комментарий',
        );
    }
}